package com.ssm.controller;


import com.ssm.pojo.User;


import com.ssm.shrio.ShiroUtils;
import com.ssm.util.EncoderByMd5;
import com.ssm.util.ResultData;
import org.apache.commons.lang3.StringUtils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 *
 * @author minghaozhi
 * @date 2017/12/23
 */
@Controller

public class LoginController {
    @Autowired
    private Producer producer;

    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletResponse response)throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
       //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    /**
     * shiro框架登录
     * @param user
     */

    /**
     * 登录
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResultData login(String loginName, String password, String captcha)throws IOException {
        String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if(!captcha.equalsIgnoreCase(kaptcha)){
            return ResultData.error("验证码不正确");
        }
//
        try{
            Subject subject = SecurityUtils.getSubject();
            //sha256加密
            try {
                password = EncoderByMd5.EncoderByMd5(password);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
            subject.login(token);
        }catch (UnknownAccountException e) {
            return ResultData.error(e.getMessage());
        }catch (IncorrectCredentialsException e) {
            return ResultData.error(e.getMessage());
        }catch (LockedAccountException e) {
            return ResultData.error(e.getMessage());
        }catch (AuthenticationException e) {
            return ResultData.error("账户验证失败");
        }

        return ResultData.ok();
    }
    /**
     * 退出
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout() {
        ShiroUtils.logout();
        return "redirect:login.jsp";
    }
}
